from model.OneScoreRankShengNei import OneScoreRankShengNei
from model.UniversityScoreRankMean import UniversityScoreRankMean
from model.Config import Config
from model.OneScoreRank import OneScoreRank
from model.OneScoreRankShengNei import OneScoreRankShengNei
from model.UniversityScoreRankMeanRate1 import UniversityScoreRankMeanRate1
from model.UniversityScoreRankMeanRate2 import UniversityScoreRankMeanRate2
from model.UniversityScoreRankMeanRate4 import UniversityScoreRankMeanRate4
import sys


# 计算每个文科院校，180-700分之间对应的概率
def rate_2():
    universityScoreRankMean = UniversityScoreRankMean()
    config = Config()
    oneScoreRank = OneScoreRank()
    oneScoreRankShengNei = OneScoreRankShengNei()
    universityScoreRankMeanRate2 = UniversityScoreRankMeanRate2()
    universityScoreRankMeanRate4 = UniversityScoreRankMeanRate4()
    year = 2023
    data = universityScoreRankMean.getListYearType(year, 2)    # 取文科的院校
    # data = universityScoreRankMean.getList2(year, 2, 19)    # 取文科的院校
    # data = universityScoreRankMean.getList4(year, 19, 2, 450000)    # 取文科的院校
    for index, row in enumerate(data):
        print(index)
        insert_data = list()
        if row['assign_mean_rank'] == 999999:
            for i in range(180, 668):
                insert_row = row.copy()
                insert_row['score'] = i
                # 开始计算概率
                insert_row['submit_rate'] = 0
                del insert_row['id']
                # print(insert_row)
                insert_data.append(insert_row)
        else:
            # 获取近两年高考分数线
            control_line_score = config.getList2(year - 1, row['batch'], row['type'], "control_line_score")
            # 控制线对应一分一档表的排名
            k0 = oneScoreRank.getOneByScore(control_line_score[0]['value'], control_line_score[0]['year'], row['type'])
            k1 = oneScoreRank.getOneByScore(control_line_score[1]['value'], control_line_score[1]['year'], row['type'])
            k = round(k0['rank'] / k1['rank'], 4)
            # 当前年份、批次、文理科类型的所有院校数量
            total_university = universityScoreRankMean.getCount2(year, row['batch'], row['type'])
            # 计算投档概率需要区分省内省外
            if row['province_code'] == 450000:
                # 比当前院校 assign_lowest_rank 小的数量
                assign_rank = row['assign_lowest_rank']
                university_rank = universityScoreRankMean.getCount4(year, row['batch'], row['type'], assign_rank)
            else:
                # 比当前院校 assign_mean_rank 小的数量
                assign_rank = row['assign_mean_rank']
                university_rank = universityScoreRankMean.getCount3(year, row['batch'], row['type'], assign_rank)
            # 遍历计算每个分数对应的概率(区间取决于当年对应的一分一档表最低分、最高分)
            for i in range(180, 668):
                insert_row = row.copy()
                insert_row['score'] = i
                # 获取用户排位，区分省内外
                if row['province_code'] == 450000:
                    user_rank_res = oneScoreRankShengNei.getOneByScore(i, year, row['type'])
                else:
                    user_rank_res = oneScoreRank.getOneByScore(i, year, row['type'])

                # 开始计算概率
                if user_rank_res:
                    user_rank = user_rank_res['total_num']
                    insert_row['submit_rate'] = calculate(assign_rank, user_rank, k, total_university,
                                                          university_rank + 1)
                else:
                    insert_row['submit_rate'] = 0

                # print(insert_row)
                del insert_row['id']
                insert_data.append(insert_row)

        universityScoreRankMeanRate4.toInsertBatch(insert_data)


def calculate(assign_rank, user_rank, k, total_university, university_rank):
    q = round(((k - 1) / (total_university - 1)) * (university_rank - 1) + 1, 4)
    rate_init = 0.6  # 初始概率 从数据库获取
    final_rate = 0
    if assign_rank:
        final_rate = round(rate_init + (1 - user_rank / (assign_rank * q)), 2)

    if final_rate > 0.95:
        final_rate = 0.95

    if final_rate < 0.10:
        final_rate = 0.10

    return final_rate * 100


def test():
    universityScoreRankMean = UniversityScoreRankMean()
    config = Config()
    oneScoreRank = OneScoreRank()
    oneScoreRankShengNei = OneScoreRankShengNei()
    universityScoreRankMeanRate2 = UniversityScoreRankMeanRate2()
    year = 2023
    data = universityScoreRankMean.getList3(year, 11, 2, 16205)  # 取理科的院校
    for index, row in enumerate(data):
        print(index, row)
        insert_data = list()
        if row['assign_mean_rank'] == 999999:
            for i in range(457, 458):
                insert_row = row.copy()
                insert_row['score'] = i
                # 开始计算概率
                insert_row['submit_rate'] = 0
                del insert_row['id']
                print(insert_row)
                insert_data.append(insert_row)
        else:
            # 获取近两年高考分数线
            control_line_score = config.getList2(year - 1, row['batch'], row['type'], "control_line_score")
            # 控制线对应一分一档表的排名
            k0 = oneScoreRank.getOneByScore(control_line_score[0]['value'], control_line_score[0]['year'], row['type'])
            k1 = oneScoreRank.getOneByScore(control_line_score[1]['value'], control_line_score[1]['year'], row['type'])
            k = round(k0['rank'] / k1['rank'], 4)
            # 当前年份、批次、文理科类型的所有院校数量
            total_university = universityScoreRankMean.getCount2(year, row['batch'], row['type'])
            # 计算投档概率需要区分省内省外
            if row['province_code'] == 450000:
                # 比当前院校 assign_lowest_rank 小的数量
                assign_rank = row['assign_lowest_rank']
                university_rank = universityScoreRankMean.getCount4(year, row['batch'], row['type'], assign_rank)
            else:
                # 比当前院校 assign_mean_rank 小的数量
                assign_rank = row['assign_mean_rank']
                university_rank = universityScoreRankMean.getCount3(year, row['batch'], row['type'], assign_rank)
            # 遍历计算每个分数对应的概率(区间取决于当年对应的一分一档表最低分、最高分)
            for i in range(457, 458):
                insert_row = row.copy()
                insert_row['score'] = i
                # 获取用户排位，区分省内外
                if row['province_code'] == 450000:
                    user_rank_res = oneScoreRankShengNei.getOneByScore(i, year, row['type'])
                else:
                    user_rank_res = oneScoreRank.getOneByScore(i, year, row['type'])

                # 开始计算概率
                if user_rank_res:
                    user_rank = user_rank_res['total_num']
                    insert_row['submit_rate'] = calculate(assign_rank, user_rank, k, total_university,
                                                          university_rank + 1)
                else:
                    insert_row['submit_rate'] = 0

                print(insert_row)
                del insert_row['id']
                insert_data.append(insert_row)

        # universityScoreRankMeanRate1.toInsertBatch(insert_data)


if __name__ == "__main__":
    rate_2()
